package com.work.service.impl;

import com.work.dao.AddressDao;
import com.work.dao.CartDao;
import com.work.dao.GoodsDao;
import com.work.dao.OrderDao;
import com.work.pojo.*;
import com.work.service.OrderService;
import com.work.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class, readOnly = true, propagation = Propagation.SUPPORTS)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private CartDao cartDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private GoodsDao goodsDao;


    @Override
    public List<Order> getAllOrder() {
        List<Order> list = orderDao.getAllOrder();
        for (Order order : list) {
            Integer uid = order.getUid();
            User user = orderDao.getUser( uid );
            order.setUsername( user.getUsername() );
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendOrder(String oid) {
        orderDao.sendOrder( oid );
    }

    @Override
    public List<Order> searchOrder(String username, Integer orderStatus) {
        User user = orderDao.getUserByname( username );
        Integer id = user.getId();
        return orderDao.searchOrder( id, orderStatus );
    }

    @Override
    public List<Order> getOrderById(Integer uid) {
        List<Order> orderById = orderDao.getOrderById( uid );
        Iterator<Order> iterator = orderById.iterator();
        while (iterator.hasNext()) {
            Order next = iterator.next();
            Order next1 = next;
            Integer aid = next1.getAid();
            Address address = addressDao.getAddressById( aid );
            String detail = address.getDetail();
            next1.setAddress( detail );
        }
        return orderById;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Order addOrder(Integer uid, int aidInt) {
        BigDecimal money = new BigDecimal( 0 );
        List<Cart> carts = cartDao.getCart( uid );

        Date date = new Date();
        String orderId = RandomUtils.createOrderId();
        for (Cart cart : carts) {
            money = money.add( cart.getMoney() );
        }
        Order order = new Order( orderId, uid, money, "1", date, aidInt );

        orderDao.addSingleOrder( order );
        for (Cart cart : carts) {
            orderDao.insertOrderDetail( new OrderDetail( 0, orderId, cart.getPid(), cart.getNum(), cart.getMoney() ) );
        }
        cartDao.clearCartAjax( uid );
        return order;
    }

    @Override
    public od getOrderDetail(String oid) {

        od od = new od();
        Order orderByoid = orderDao.getOrderByoId( oid );
        int aid = orderByoid.getAid();
        Address addressById = addressDao.getAddressById( aid );
        List<OrderDetail> orderDetails = orderDao.listOrderDetailBy( oid );
        for (OrderDetail orderDetail : orderDetails) {
            int pid = orderDetail.getPid();
            Goods goods = goodsDao.getGoods( pid );
            orderDetail.setGoods( goods );
        }
        od.setOrder( orderByoid );
        od.setAddress( addressById );
        od.setList( orderDetails );
        return od;

    }

    @Override
    public void changeStatus(String oid) {
        orderDao.changeStatus(oid);
    }
}
